#coding=utf-8
import unittest
from selenium import webdriver
from appium import webdriver
from time import sleep
from nav_plugin_op import nav_plugin_factory
from selenium.common.exceptions import WebDriverException
from Public import BasePage
from Tool import usefulTools
from Public import BaseConstant
import settings
import lanuch_exit
import loginEgovaMobile


#实例化基础数据

navOpenFactory = nav_plugin_factory()
userfulTool_instamces = usefulTools.userfulToolsFactory()
element_oprate_instance = BasePage.element_Oprate()
element_find_instance = BasePage.BasicAction()
Leaderassign_Paramter_instances = BaseConstant.Leaderassign_Paramter
lanuch_exit_app = lanuch_exit.lanuch_exit_factory()
login_mobile_instances = loginEgovaMobile.login_factory()
dealHelper_parameter_instances = BaseConstant.DisposeCase

# nav_name = "工作"
# plugin_name = "领导交办"
nav_name = ["导航栏工作-for-领导交办",'xpath',"//android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.FrameLayout/android.widget.RadioButton"]
plugin_name = ["插件列表-领导交办",'name','领导交办']
reply_keys_name = "请处理"
tip_content_keys = "自动化创建-领导交办任务"

Receiver_infor = ''

class leader_assign_factory():

	def open_leaderassign_plug(self,driver,file_path):
		#查找并点击导航栏的插件
		while (True):
			if element_oprate_instance.element_display_commit(plugin_name, settings.pic_file_path):
				element_oprate_instance.element_click(plugin_name, settings.pic_file_path)
				break
			else:
				if element_oprate_instance.element_display_commit(nav_name, settings.pic_file_path):
					driver.swipe(400, 400, 400, 50, 1200)
					print u'当前在功能列表界面，进行滚动操作'
				else:
					print u'当前不在功能列表界面，先调用返回操作'
					try:
						element_oprate_instance.element_click(Leaderassign_Paramter_instances.exit_id,settings.pic_file_path)
					except Exception, e:
						print e
						print u'执行返回操作'
		sleep(5)
		element_oprate_instance.element_save_pic(settings.pic_file_path,'open_leaderassign_plug_successful')



	def reply_leader_assign(self,file_path):
		try:
			sleep(8)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.menuview_image_id, file_path)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.need_dealt_name, file_path)
			sleep(3)
			#点击回复
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.reply_xpath, file_path)
			sleep(1)
			#输入回复内容
			element_oprate_instance.element_send_keys(Leaderassign_Paramter_instances.replycontent_id,reply_keys_name.decode("utf-8"), file_path)
			#如果打开惯用语添加界面，则进行添加
			if element_oprate_instance.element_display_commit(Leaderassign_Paramter_instances.phrase_add_OK_button,settings.pic_file_path):
				element_oprate_instance.element_click(Leaderassign_Paramter_instances.phrase_add_OK_button,settings.pic_file_path)
				#再次输入内容
				element_oprate_instance.element_send_keys(Leaderassign_Paramter_instances.replycontent_id,reply_keys_name.decode("utf-8"), file_path)
			# element_find_instance.find_element(Leaderassign_Paramter_instances.replycontent_id,settings.pic_file_path).send_keys(reply_keys_name.decode("utf-8"))
			element_oprate_instance.element_save_pic(file_path, 'reply_leader_assign1')
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.btn_reply_id, file_path)
			sleep(2)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.menuview_image_id, file_path)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.all_name, file_path)
			sleep(2)
			element_oprate_instance.element_save_pic(file_path, 'reply_leader_assign2')

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'reply_leader_assign_fail')
			raise		

	def send_leader_assign(self,file_path):

		try:
			sleep(2)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.menuview_image_id, file_path)
			sleep(2)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.need_dealt_name, file_path)
			#点击回复
			sleep(2)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.send_xpath, file_path)
			sleep(1)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.add_members_id, file_path)

			sleep(2)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.contact_select_name, file_path)

			sleep(2)
			# element_oprate_instance.element_click(Leaderassign_Paramter_instances.contact_select_xpath, file_path)
			# 选择部门，
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.choose_unit, settings.pic_file_path)
			# 勾选第一个部门
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.first_unit_checkbox,settings.pic_file_path)
			# 点击确认按钮
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.contact_confirm_unit, file_path)
			# element_oprate_instance.element_click(Leaderassign_Paramter_instances.contact_confirm_id, file_path)

 			sleep(2)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.tip_content_id, file_path)
			element_oprate_instance.element_send_keys(Leaderassign_Paramter_instances.tip_content_id,reply_keys_name.decode("utf-8"), file_path)

			element_oprate_instance.element_save_pic(file_path, 'send_leader_assign1')
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.Btn_send_id, file_path)
			sleep(2)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.menuview_image_id, file_path)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.all_name, file_path)

			sleep(2)
			element_oprate_instance.element_save_pic(file_path, 'send_leader_assign2')

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'reply_leader_assign_fail')
			raise		

	def add_leader_assign(self,driver,file_path):
		try:
			#新建领导交办
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.newassign_id, file_path)
			#选择接收人员
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.recive_members_id, file_path)
			sleep(2)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.contact_select_name, file_path)
			#选择部门，
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.choose_unit,settings.pic_file_path)
			#勾选第一个部门
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.first_unit_checkbox,settings.pic_file_path)
			#点击确认按钮
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.contact_confirm_unit, file_path)
			#输入交办内容
			sleep(2)
			Receiver_infor = element_find_instance.find_element(Leaderassign_Paramter_instances.Receiver_area,settings.pic_file_path).get_attribute('text')
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.tip_content_id, file_path)
			element_oprate_instance.element_send_keys(Leaderassign_Paramter_instances.tip_content_id,tip_content_keys.decode("utf-8"), file_path)
			#选择时间,以一天为例
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.b_12h_id, file_path)
			element_oprate_instance.element_save_pic(file_path, 'add_leader_assign_successful')
			#提交
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.btn_send_id, file_path)
			sleep(3)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.exit_id, file_path)
			element_oprate_instance.element_click(Leaderassign_Paramter_instances.Exit_id, file_path)

			return Receiver_infor

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'add_leader_assign_fail')
			raise

leader_assign_factory_instances = leader_assign_factory()

class leader_assign_module():
	#进行领导交办任务转发操作，先获取到接收人，然后再进行操作
	def leaderTask_Forward_function(self):
		# 先进行检查当前的页面是否为功能列表界面
		while (True):
			if element_oprate_instance.element_display_commit(nav_name, settings.pic_file_path):
				print u'当前在功能列表界面，可进行操作'
				break
			else:
				print u'当前不在功能列表界面，先调用返回操作'
				try:
					element_oprate_instance.element_click(Leaderassign_Paramter_instances.exit_id,settings.pic_file_path)
				except Exception, e:
					print e
					print u'执行返回操作'
		#调用打开插件
		leader_assign_factory_instances.open_leaderassign_plug(BasePage.Base.driver,settings.pic_file_path)
		#获取接收人员的信息
		Receiver_infor = element_find_instance.find_element(Leaderassign_Paramter_instances.leaderassign_receiver,settings.pic_file_path).get_attribute('text')
		Receiver_user_name = userfulTool_instamces.SubString_handle(Receiver_infor,'要求','等')
		print u'获取接收人员信息：' + str(Receiver_infor)
		print u'获取接收人员信息：' + str(Receiver_user_name)
		#调用退出功能
		lanuch_exit_app.close_app(BasePage.Base.driver, settings.pic_file_path)
		#调用登录功能
		lanuch_exit_app.lanuch_app(BasePage.Base.driver, settings.pic_file_path)
		login_mobile_instances.login_system(BasePage.Base.driver, Receiver_user_name, '', settings.pic_file_path)
		# 调用打开插件
		leader_assign_factory_instances.open_leaderassign_plug(BasePage.Base.driver, settings.pic_file_path)
		#调用基础的 转发功能
		leader_assign_factory_instances.send_leader_assign(settings.pic_file_path)
		#调用返回，返回功能列表
		element_oprate_instance.element_click(dealHelper_parameter_instances.dealhelper_back_button,settings.pic_file_path)

	def leaderTask_reply_function(self):
		# 先进行检查当前的页面是否为功能列表界面
		while (True):
			if element_oprate_instance.element_display_commit(nav_name, settings.pic_file_path):
				print u'当前在功能列表界面，可进行操作'
				break
			else:
				print u'当前不在功能列表界面，先调用返回操作'
				try:
					element_oprate_instance.element_click(Leaderassign_Paramter_instances.exit_id,settings.pic_file_path)
				except Exception, e:
					print e
					print u'执行返回操作'
		#调用打开插件
		leader_assign_factory_instances.open_leaderassign_plug(BasePage.Base.driver,settings.pic_file_path)
		#获取接收人员的信息
		Receiver_infor = element_find_instance.find_element(Leaderassign_Paramter_instances.leaderassign_Forwarding_personnel,settings.pic_file_path).get_attribute('text')
		# Receiver_user_name = userfulTool_instamces.SubString_handle(Receiver_infor,'要求','等')
		Receiver_user_name = Receiver_infor
		print u'获取接收人员信息：' + str(Receiver_infor)
		print u'获取接收人员信息：' + str(Receiver_user_name)
		#调用退出功能
		lanuch_exit_app.close_app(BasePage.Base.driver, settings.pic_file_path)
		#调用登录功能
		lanuch_exit_app.lanuch_app(BasePage.Base.driver, settings.pic_file_path)
		login_mobile_instances.login_system(BasePage.Base.driver, Receiver_user_name, '', settings.pic_file_path)
		# 调用打开插件
		leader_assign_factory_instances.open_leaderassign_plug(BasePage.Base.driver, settings.pic_file_path)
		#调用基础的 回复功能
		leader_assign_factory_instances.reply_leader_assign(settings.pic_file_path)



if __name__ == '__main__':
	unittest.main()		


